/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.sync;

import java.io.File;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import xmlasm.bo.FeedBO;
import xmlasm.bo.FeedbackBO;
import xmlasm.bo.UserBO;

/**
 *
 * @author Hoang
 */
public class SynchronizeThread extends Thread {

    private boolean runInBackground;

    public boolean isRunInBackground() {
        return runInBackground;
    }

    public void setRunInBackground(boolean runInBackground) {
        this.runInBackground = runInBackground;
    }

    @Override
    public void run() {
        while (runInBackground) {
            try {
                super.run();
                //Nghi 5 p
                this.sleep(5 * 60 * 1000);
                //Goi cai dong bo trong nay
                Date date = new Date();
                System.out.println("Begin Sync XML From DB at: " + date.toString());

                //Neu file da ton tai, tuc la da duoc load, moi thuc hien sync
                File f = new File(FilePathHolder.getInstance().getUserXMLPath());
                if (f.exists()) {
                    UserBO.loadUsersAndMarshall();
                    System.out.println("Sync user.xml");
                }

                f = new File(FilePathHolder.getInstance().getFeedbackXMLPath());
                if (f.exists()) {
                    FeedbackBO.loadFeedbackAndMarshall();
                    System.out.println("Sync feedback.xml");
                }

                FeedBO.loadFeedsAndMarshall();
                System.out.println("Sync feed.xml");

                System.out.println("Sync Complete");
            } catch (InterruptedException ex) {
                Logger.getLogger(SynchronizeThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
